package com.hengtongxing.hejiayun.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期相关操作的工具类
 *
 * @author
 */
@SuppressLint("SimpleDateFormat")
public class DateTimeUtil {

    public static final String DEFAULT_TIME = " 00:00:00.000";//
    private static final String[] CN_DAY_OF_WEEK = new String[]{"", "日", "一", "二", "三", "四", "五", "六"};


    /**
     * 计算当月最后一天,返回字符串
     */
    public static String getDefaultDay() {
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
        lastDate.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
        lastDate.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(lastDate.getTime());
        return str != null ? str : "";
    }


    /**
     * 获得日期的天和星期【1号（周四）】
     *
     * @param date
     * @return
     */
    public static String getDateWithDayWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        return (String.format("%02d", day) + "号（周" + CN_DAY_OF_WEEK[week] + "）");
    }

    /**
     * 获取日期的格式如：3月21(四)
     *
     * @param date
     * @return
     */
    public static String getDateMonthAndDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String mMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        return mMonth + "." + mDay + "(" + CN_DAY_OF_WEEK[week] + ")";
    }

    /**
     * 获取当月第一天
     */
    public static String getFirstDayOfMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
        String str = format.format(lastDate.getTime());
        return str != null ? str : "";
    }


    /**
     * 传参数获取这个月的最后一天
     *
     * @param strDate
     * @return
     */
    public static String getLastDayOfMonth(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return "";
        }
        int day = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar lastDate = Calendar.getInstance();
        try {
            lastDate.setTime(sdf.parse(strDate));
            day = lastDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdf.format(lastDate.getTime()) + "-" + day;
    }


    /**
     * 传参数获取这个月的最后一天
     *
     * @param date
     * @return int
     */
    public static int getLastDayOfMonth(Date date) {
        if (date == null) {
            return 0;
        }
        int day = 0;
        Calendar lastDate = Calendar.getInstance();
        try {
            lastDate.setTime(date);
            day = lastDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 传参数获取这个月的第一天
     *
     * @param strDate
     * @return
     */
    public static String getFirstDayOfMonth(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return "";
        }
        String day = "01";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar lastDate = Calendar.getInstance();
        try {
            lastDate.setTime(sdf.parse(strDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdf.format(lastDate.getTime()) + "-" + day;
    }

    /**
     * 获取当天的日期
     *
     * @return
     */
    public static String getToDayDate() {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 获取当天的日期
     *
     * @return
     */
    public static String getToDayDate2() {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        return format.format(date);
    }

    /**
     * 获取当月的日期
     *
     * @return
     */
    public static String getMonthDate() {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    /**
     * 获取当月的日期
     *
     * @return
     */
    public static String getMonthDate2() {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM");
        return format.format(date);
    }

    /**
     * 获取明天的日期
     *
     * @return
     */
    public static String getNextDate() {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 获取当前日期的前后任意一天的日期
     *
     * @return
     */
    public static Date getSomeDateType(int dayCount) {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayCount);// 把日期往后/前增加一天.整数往后推,负数往前移动
        return calendar.getTime();
    }

    /**
     * 从APP开始使用第一天算起，计算当前日期的上一周的最后一天
     */
    public static Date getLastWeekLastDay(Date appStartDate) {
        int nd = 1000 * 60 * 60 * 24;//一天的毫秒值
        int day = (int) ((new Date().getTime() - appStartDate.getTime()) / nd);
        //        System.out.println("当前天数差——" + day);
        int dayCount = day % 7;
        Date lastDay = getSomeDateType(-dayCount - 1);
        //        System.out.println("上周最后一天——" + format(lastDay));
        return lastDay;
    }

    /**
     * 获取当前日期的前后任意一天的日期
     *
     * @return
     */
    public static String getSomeDate(int dayCount) {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayCount);// 把日期往后/前增加一天.整数往后推,负数往前移动
        date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 获取当前日期的前后任意一天的日期
     *
     * @return
     */
    public static String getSomeDate2(int dayCount) {
        Date date = new Date();// 取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayCount);// 把日期往后/前增加一天.整数往后推,负数往前移动
        date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("MM.dd");
        return format.format(date);
    }

    /**
     * 获取指定日期的前后任意一天的日期
     *
     * @param dateStr  yyyy-MM-dd
     * @param dayCount 把日期往后/前增加一天.正数往后推,负数往前移动
     * @return yyyy-MM-dd
     */
    public static String getSomeDate(String dateStr, int dayCount) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            Date date = format.parse(dateStr);
            calendar.setTime(date);
            calendar.add(Calendar.DATE, dayCount);
            date = calendar.getTime();
            return format.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定日期的前后任意一天的日期
     *
     * @param date     yyyy-MM-dd
     * @param dayCount 把日期往后/前增加一天.正数往后推,负数往前移动
     * @return yyyy-MM-dd
     */
    public static Date getSomeDate(Date date, int dayCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayCount);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取月日
     *
     * @param date
     * @return
     */
    public static String getSomeMoneyDay(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        date = calendar.getTime();
        return format.format(date);
    }

    /**
     * 获取星期几
     *
     * @param date
     * @return
     */
    public static String getSomeDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getCNDayOfWeek(calendar);
    }

    /**
     * 获取指定日期的前后任意年的日期
     *
     * @param date  yyyy-MM-dd
     * @param years 把日期往后/前增加一年.正数往后推,负数往前移动
     */
    public static Date getDateAfterYears(Date date, int years) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);

        date = calendar.getTime();

        return date;
    }

    /**
     * 根据一个星期的第几天，获取中文星期几
     *
     * @param dayOfWeek
     * @return
     */
    public static String getCNDayOfWeek(int dayOfWeek) {
        return "星期" + CN_DAY_OF_WEEK[dayOfWeek];
    }

    public static String getCNDayOfWeek(Calendar c) {
        return getCNDayOfWeek(c.get(Calendar.DAY_OF_WEEK));
    }

    /**
     * 获取当前的时间（上午/下午 时/分）
     *
     * @return
     */
    public static String getNowTime() {
        String time = null;
        Calendar calendar = Calendar.getInstance();
        if ((calendar.get(Calendar.AM_PM)) == 0) {
            time = "上午 " + format4(calendar.getTime());
        } else if ((calendar.get(Calendar.AM_PM)) == 1) {
            time = "下午 " + format4(calendar.getTime());
        }
        return time;
    }


    /**
     * "MM/dd"
     */
    public static String format18(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format18 = new SimpleDateFormat("MM/dd");
        return format18.format(date);
    }

    /**
     * "MM/dd"
     */
    public static Date parse18(String str) {
        try {
            SimpleDateFormat format18 = new SimpleDateFormat("MM/dd");
            return format18.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy"
     */
    public static String format17(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format17 = new SimpleDateFormat("yyyy");
        return format17.format(date);
    }

    /**
     * "yyyy"
     */
    public static Date parse17(String str) {
        try {
            SimpleDateFormat format17 = new SimpleDateFormat("yyyy");
            return format17.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * "yyyy-MM-dd"
     */
    public static String format(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * “yyyy.MM.dd”
     *
     * @param date
     * @return
     */
    public static String format13(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format13 = new SimpleDateFormat("yyyy.MM.dd");
        return format13.format(date);
    }

    /**
     * “yyyy.MM.dd”
     *
     * @param date
     * @return
     */
    public static String format22(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format13 = new SimpleDateFormat("yyyy.MM");
        return format13.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss.SSS
     *
     * @param date
     * @return
     */
    public static String format14(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format14 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format14.format(date);
    }

    public static String format15(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format15 = new SimpleDateFormat("yyyy年MM月dd日");
        return format15.format(date);
    }

    public static String format25(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format15 = new SimpleDateFormat("MM月dd日");
        return format15.format(date);
    }

    /**
     * yyyy-MM
     */
    public static String format16(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format16 = new SimpleDateFormat("yyyy-MM");
        return format16.format(date);
    }

    /**
     * yyyy-MM
     */
    public static Date parse16(String str) {
        try {
            SimpleDateFormat format16 = new SimpleDateFormat("yyyy-MM");
            return format16.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * yyyy.MM.dd HH:mm:ss.SSS
     */
    public static Date parse20(String str) {
        try {
            SimpleDateFormat format20 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS");
            return format20.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse13(String str) {
        try {
            SimpleDateFormat format13 = new SimpleDateFormat("yyyy.MM.dd");
            return format13.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * yyyy-MM-dd HH:mm:ss.SSS
     *
     * @param str yyyy-MM-dd HH:mm:ss.SSS
     * @return date
     */
    public static Date parse14(String str) {
        try {
            SimpleDateFormat format14 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            return format14.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse15(String str) {
        try {
            SimpleDateFormat format15 = new SimpleDateFormat("yyyy年MM月dd日");
            return format15.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy-MM-dd"
     */
    public static Date parse(String str) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy-MM-dd"
     */
    public static Date parse22(String str) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy-MM-dd"
     */
    public static Date parse23(String str) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM");
            return format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyyMMdd"
     */
    public static String format1(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        return format1.format(date);
    }

    /**
     * "yyyyMMdd"
     */
    public static Date parse1(String str) {
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
            return format1.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyyMMddHHmmss"
     */
    public static String format2(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmss");
        return format2.format(date);
    }

    /**
     * "yyyyMMddHHmmss"
     */
    public static Date parse2(String str) {
        try {
            SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmss");
            return format2.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yy-MM-dd"
     */
    public static String format3(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format3 = new SimpleDateFormat("yy-MM-dd");
        return format3.format(date);
    }

    /**
     * "yy-MM-dd"
     */
    public static Date parse3(String str) {
        try {
            SimpleDateFormat format3 = new SimpleDateFormat("yy-MM-dd");
            return format3.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "HH:mm"
     */
    public static String format4(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format4 = new SimpleDateFormat("HH:mm");
        return format4.format(date);
    }

    //<xujp to add
    public static String format_HH(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format_hh = new SimpleDateFormat("HH");
        return format_hh.format(date);
    }
    //xujp to add>

    /**
     * "HH:mm"
     */
    public static Date parse4(String str) {
        try {
            SimpleDateFormat format4 = new SimpleDateFormat("HH:mm");
            return format4.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy-MM-dd HH:mm:ss.SSS"
     */
    public static String format5(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format5.format(date);
    }

    /**
     * "yyyy-MM-dd HH:mm:ss.SSS"
     */
    public static Date parse5(String str) {
        try {
            SimpleDateFormat format5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            return format5.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "MM dd yyyy h:mm"
     */
    public static String format6(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format6 = new SimpleDateFormat("MM dd yyyy h:mm");
        return format6.format(date);
    }

    /**
     * "MM dd yyyy h:mm"
     */
    public static Date parse6(String str) {
        try {
            SimpleDateFormat format6 = new SimpleDateFormat("MM dd yyyy h:mm");
            return format6.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"
     */
    public static String format7(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format7 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format7.format(date);
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"
     */
    public static Date parse7(String str) {
        try {
            SimpleDateFormat format7 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return format7.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * "yyyy/MM/dd HH:mm:ss"
     */
    public static String format8(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format8 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return format8.format(date);
    }

    public static String format8_brief(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format8_brief = new SimpleDateFormat("yyyy/MM/dd");
        return format8_brief.format(date);
    }

    /**
     * "yyyy/MM/dd HH:mm:ss"
     */
    public static Date parse8(String str) {
        try {
            SimpleDateFormat format8 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            return format8.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse8_brief(String str) {
        try {
            SimpleDateFormat format8_brief = new SimpleDateFormat("yyyy/MM/dd");
            return format8_brief.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * yyyy.MM.dd
     *
     * @param date
     * @return
     */
    public static String format9(Date date) {
        if (date == null) {
            return null;
        }
        return format(date).replace("-", ".");
    }

    /**
     * HH:mm:ss
     */
    public static String format10(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format10 = new SimpleDateFormat("HH:mm:ss");
        return format10.format(date);
    }

    /**
     * HH:mm:ss
     */
    public static Date parse10(String str) {
        try {
            SimpleDateFormat format10 = new SimpleDateFormat("HH:mm:ss");
            return format10.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * yyyy-MM-dd HH:mm
     */
    public static String format11(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format11 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format11.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm
     */
    public static Date parse11(String str) {
        try {
            SimpleDateFormat format11 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return format11.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * yyyy年MM月dd日 HH:mm
     */
    public static String format12(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format12 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        return format12.format(date);
    }

    /**
     * yyyy年MM月dd日 HH:mm
     */
    public static Date parse12(String str) {
        try {
            SimpleDateFormat format12 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            return format12.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 比较两个日期大小
     *
     * @param date1
     * @param date2
     * @return true：date1大，false：date2大
     */
    public static boolean compareDay(Date date1, Date date2) {
        return date1.after(date2);
    }

    /**
     * 比较两个日期的大小
     *
     * @param beforeDay
     * @param afterDay
     * @param beforeFormat
     * @param afterFormat
     * @return
     * @throws ParseException
     */
    public static boolean compareDay(String beforeDay, String afterDay, String beforeFormat, String afterFormat) throws ParseException {
        beforeDay = format2Str(beforeDay, beforeFormat, "yyyyMMdd");
        afterDay = format2Str(afterDay, afterFormat, "yyyyMMdd");
        int beforeInt = Integer.parseInt(beforeDay);
        int afterInt = Integer.parseInt(afterDay);
        return beforeInt < afterInt;
    }

    /**
     * 如"yyyy-MM-dd" ==> "yyyyMMdd"
     *
     * @param dateStr      需要格式化的时间字符串
     * @param beforeFormat 格式化前的格式
     * @param afterFormat  格式化后的格式
     * @return
     * @throws ParseException
     */
    public static String format2Str(String dateStr, String beforeFormat, String afterFormat) throws ParseException {
        SimpleDateFormat formatBef = new SimpleDateFormat(beforeFormat);
        SimpleDateFormat formatAft = new SimpleDateFormat(afterFormat);
        Date befDate = formatBef.parse(dateStr);
        String afterDate = formatAft.format(befDate);
        return afterDate;
    }

    /**
     * 获取指定时间的后面addNum天
     *
     * @param dateStr
     * @param addNum
     * @return
     */
    public static String getDate(String dateStr, int addNum) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = null;
        Date date = null;
        try {
            format = new SimpleDateFormat("yyyy.MM.dd");
            date = format.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, addNum);// 加startDay天
        String datas = format.format(calendar.getTime());
        return datas;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getNow() {
        return new Date();
    }

    /**
     * 判断这个日期跟当前时间相差多少天？
     *
     * @param date
     * @return
     */
    public static int getBetweenDay(Date date) {
        date = parse(format(date));
        Date nowDate = parse(format(new Date()));

        long time1 = date.getTime();
        long time2 = nowDate.getTime();

        //<xujp to add
        long betweenDays = (time2 - time1) / (24 * 60 * 60 * 1000);
        //xujp to add>

        //超过200年的置为0
        if (betweenDays > 365 * 200 || betweenDays < -365 * 200) {
            betweenDays = 0;
        }

        return (int) betweenDays;

    }

    /**
     * 判断这个日期跟这个日期相差多少天
     *
     * @return
     */
    public static int getBetweenDay_2(Date date1, Date date2) {

        date1 = parse(format(date1));
        date2 = parse(format(date2));

        long time1 = date1.getTime();
        long time2 = date2.getTime();

        long betweenDays = (time2 - time1) / (24 * 60 * 60 * 1000);

        //超过200年的置为0
        if (betweenDays > 365 * 200 || betweenDays < -365 * 200) {
            betweenDays = 0;
        }

        return (int) betweenDays;
    }

    /**
     * @param date1 yyyy-mm-dd
     * @param date2 yyyy-mm-dd
     */
    public static int getBetweenDay(Date date1, Date date2) {

        date1 = parse(format(date1));
        date2 = parse(format(date2));

        long time1 = date1.getTime();
        long time2 = date2.getTime();

        long betweenDays = (time2 - time1) / (24 * 60 * 60 * 1000);

        //超过200年的置为0
        if (betweenDays > 365 * 200 || betweenDays < -365 * 200) {
            betweenDays = 0;
        }

        return Math.abs((int) betweenDays);
    }


    /**
     * @param dateStr 日期 格式为"yyyy-MM-dd HH:mm:ss.SSS"
     * @return
     * @throws ParseException
     */
    public static long dateToTimeMillis(String dateStr) throws ParseException {
        SimpleDateFormat format5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format5.parse(dateStr).getTime();
    }


    public static Date timeAddHours(Date date, float hours) {

        long time = date.getTime();
        long time1 = (long) (3600000 * hours);

        String s = format4(new Date(time + time1));

        return parse4(s);
    }

    /**
     * 判断两个日期是否是同一天
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
        return isSameDate;
    }

    /**
     * 计算两个日期之间有几年
     * endDate必须大于或等于startDate
     */
    public static int getYears(Date startDate, Date endDate) {
        int years = 0;
        int months = 0;
        //        int days = 0;
        //        final long YEAR_MILLIS = 1000 * 60 * 60 * 24 * 365;
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startDate);
        end.setTime(endDate);
        int y0 = start.get(Calendar.YEAR);
        int y1 = end.get(Calendar.YEAR);
        int m0 = start.get(Calendar.MONTH);
        int m1 = end.get(Calendar.MONTH);
        //        int d0 = start.get(Calendar.DAY_OF_MONTH);
        //        int d1 = end.get(Calendar.DAY_OF_MONTH);
        years = y1 - y0;
        months = m1 - m0;
        //        days = d1 - d0;
        //        if (days < 0) {
        //            months--;
        //        }
        if (months < 0) {
            years--;
        }
        //        years = (int) ((start.getTimeInMillis() - end.getTimeInMillis()) / YEAR_MILLIS);
        return years;
    }

    public static int getMonths(Date newDate, Date oldDate) {
        int monthResult = 0;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(newDate);
        c2.setTime(oldDate);
        if (c1.getTimeInMillis() < c2.getTimeInMillis()) {
            return 0;
        }
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);

        int yearInterval = year1 - year2;
        //如果月份1小于月份2，或者月份相等且日1小于日2，则年差值减一
        if (month1 < month2 || (month1 == month2 && day1 < day2)) {
            yearInterval--;
        }
        //计算月数差
        int monthInterval = 0;
        if (month1 < month2) {
            monthInterval = (month1 + 12) - month2;
        } else {
            monthInterval = month1 - month2;
        }
        if (day1 < day2) {
            monthInterval--;
        }
        monthResult = yearInterval * 12 + monthInterval;
        return monthResult;
    }

    //< ysb to add

    /**
     * 获取指定时间之前或之后几小时 minute
     */
    public static Date getTimeByHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //24小时制
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }
    /*获取指定时间之前或之后几天*/

    public static int getTimeByDay(Date date, int day) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //24小时制
        calendar.add(Calendar.DATE, day);

        //        return calendar.getTime();
        return calendar.get(Calendar.DAY_OF_MONTH);

    }


    /*获取指定时间之前或之后几天*/

    public static Date getDateTimeByDay(Date date, int day) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //24小时制
        calendar.add(Calendar.DATE, day);

        return calendar.getTime();

    }


    public static int getDayofweek(Date date) {
        Calendar cal = Calendar.getInstance();
        //   cal.setTime(new Date(System.currentTimeMillis()));
        if (date == null) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            cal.setTime(date);
        }
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * "yyyy年MM月"
     */
    public static String format19(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format19 = new SimpleDateFormat("yyyy年MM月");
        return format19.format(date);
    }

    /**
     * "yyyy年MM月"
     */
    public static Date parse19(String str) {
        try {
            SimpleDateFormat format19 = new SimpleDateFormat("yyyy年MM月");
            return format19.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    // ysb to add >

    public static int getDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        if (date == null) {
            return -1;
        } else {
            c.setTime(date);
            return c.get(Calendar.DAY_OF_MONTH);
        }
    }

    //<gl to add

    /**
     * 获取指定时间之前或之后多少分钟的时间
     */
    public static Date getTimeByMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    //gl to add>
    //< ysb to add
    public static Date getDateByDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }
    // ysb to add >


    // gl to add>

    /**
     * 计算两个日期之间相差的小时
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */

    public static int hoursBetween(Date smdate, Date bdate) throws ParseException {
        if (smdate.getTime() > bdate.getTime()) {
            Date temp = smdate;
            smdate = bdate;
            bdate = temp;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_hours = (time2 - time1) / (1000 * 3600);

        return Integer.parseInt(String.valueOf(between_hours));
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        smdate = format.parse(format.format(smdate));
        bdate = format.parse(format.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(format.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(format.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * @Description: TODO日期加几天
     */
    public static String dateAddDays(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);// 今天+days天
        Date tomorrow = c.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(tomorrow);
    }

    public static Date getStartDate(Date date) {
        String format_str = format(date);
        return parse5(format_str + " 00:00:00.000");
    }

    public static Date getEndDate(Date date) {
        String format_str = format(date);
        return parse5(format_str + " 23:59:59.999");
    }

    public static String dateAddDaysBy9(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);// 今天+days天
        Date tomorrow = c.getTime();
        return format9(tomorrow);
    }

    //毫秒转秒
    public static String long2String(long time) {
        //毫秒转秒
        int sec = (int) time / 1000;
        int min = sec / 60;    //分钟
        sec = sec % 60;        //秒
        if (min < 10) {    //分钟补0
            if (sec < 10) {    //秒补0
                return "0" + min + ":0" + sec;
            } else {
                return "0" + min + ":" + sec;
            }
        } else {
            if (sec < 10) {    //秒补0
                return min + ":0" + sec;
            } else {
                return min + ":" + sec;
            }
        }
    }

    /**
     * 毫秒转化时分秒毫秒
     *
     * @param ms
     * @return
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "d");
        }
        if (hour > 0) {
            sb.append(hour + "h");
        }
        if (minute > 0) {
            sb.append(minute + "′");
        }
        if (second > 0) {
            sb.append(second + "″");
        }
        return sb.toString();
    }

    /*
     * 将秒数转为时分秒
     * */
    public static String change(int second) {
        String time;

        if (second < 60) {

            time = String.format("00:%02d", second % 60);

        } else if (second < 3600) {

            time = String.format("%02d:%02d", second / 60, second % 60);

        } else {

            time = String.format("%02d:%02d:%02d", second / 3600, second % 3600 / 60, second % 60);

        }
        return time;
    }

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(Date date) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();

        int currentDayIndex = calendar.get(Calendar.DAY_OF_YEAR);
        int currentYear = calendar.get(Calendar.YEAR);

        calendar.setTime(date);
        int msgYear = calendar.get(Calendar.YEAR);
        int msgDayIndex = calendar.get(Calendar.DAY_OF_YEAR);
        int msgMinute = calendar.get(Calendar.MINUTE);

        String msgTimeStr = calendar.get(Calendar.HOUR_OF_DAY) + ":";

        if (msgMinute < 10) {
            msgTimeStr = msgTimeStr + "0" + msgMinute;
        } else {
            msgTimeStr = msgTimeStr + msgMinute;
        }

        int msgDayInWeek = calendar.get(Calendar.DAY_OF_WEEK);

        if (currentDayIndex == msgDayIndex) {
            return msgTimeStr;
        } else {
            if (currentDayIndex - msgDayIndex == 1 && currentYear == msgYear) {
                msgTimeStr = "昨天 " + msgTimeStr;
            } else if (currentDayIndex - msgDayIndex > 1 && currentYear == msgYear) { //本年消息
                //不同周显示具体月，日，注意函数：calendar.get(Calendar.MONTH) 一月对应0，十二月对应11
                msgTimeStr = (Integer.valueOf(calendar.get(Calendar.MONTH) + 1)) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日 " + msgTimeStr + " ";
            } else { // 1、非正常时间，如currentYear < msgYear，或者currentDayIndex < msgDayIndex
                //2、非本年消息（currentYear > msgYear），如：历史消息是2018，今年是2019，显示年、月、日
                msgTimeStr = msgYear + "年" + (Integer.valueOf(calendar.get(Calendar.MONTH) + 1)) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日" + msgTimeStr + " ";
            }
        }
        return msgTimeStr;
    }
}
